package com.treasure.hunt.task

import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Environment
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.LogUtils
import com.module.lemlin.base.BaseAbstractActivity
import com.module.lemlin.mode.ViewModeActivity
import com.module.lemlin.rxhttp.Status
import com.module.lemlin.rxhttp.msg
import com.module.lemlin.util.qqShareImage
import com.module.lemlin.util.wxAuthorize
import com.module.lemlin.util.wxShareImage
import com.treasure.hunt.R
import com.treasure.hunt.app.AppConstance
import com.treasure.hunt.getDiskCacheDirectoryImage
import com.treasure.hunt.mine.MineBuddyActivity
import com.treasure.hunt.mode.OtherViewMode
import com.treasure.hunt.popup.DialogSharePopup
import kotlinx.android.synthetic.main.activity_task_invite.*
import rxhttp.RxHttp
import java.io.File


class TaskInviteActivity : ViewModeActivity<OtherViewMode>() {

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, TaskInviteActivity::class.java)
            activity.startActivity(intent)
        }
    }

    private var shareType: Int = 1

    private val sharePopup: DialogSharePopup by lazy {
        DialogSharePopup(context = this) { view ->
            when (view.id) {
                R.id.tvDialogShareWX -> shareType = 1
                R.id.tvDialogShareQQ -> shareType = 2
            }
            viewModel.otherShareImage(2)
        }
    }

    override val layoutResId: Int
        get() = R.layout.activity_task_invite

    override fun viewModel(): OtherViewMode {
        return createViewModel()
    }

    override fun stateBar(): StatusBar {
        return StatusBar(
            isLightMode = false,
            bottomColor = R.color.FF_390060,
            view = flTaskInviteBar
        )
    }

    override fun initView() {
        ivTaskInviteBack.setOnClickListener { finish() }
        tvTaskInviteStart.setOnClickListener {
            sharePopup.showPopupWindow()
        }

        tvTaskInviteBuddy.setOnClickListener {
            MineBuddyActivity.open(activity = this)
        }
    }

    override fun initViewMode() {
        viewModel.otherUrlLiveData.observe(this, Observer { response ->
            if (response.status == Status.FAILURE)
                toast(response.throwable?.msg ?: "未知错误")
            if (response.status != Status.SUCCESS)
                return@Observer
            response.httpData?.data?.let { image ->
                if (image.url.isNullOrEmpty()) {
                    toast("分享图片地址为空")
                } else {
                    val isHttps: Boolean = image.url.substring(0, 5).contains("http") ||
                            image.url.substring(0, 5).contains("https")
                    RxHttp.get("${if (isHttps) "" else AppConstance.URL_IMAGE}${image.url}")
                        .asDownload("${getDiskCacheDirectoryImage(this).path}/share.png")
                        .subscribe({
                            runOnUiThread {
                                if (shareType == 1) {
                                    wxShareImage(it)
                                }
                                if (shareType == 2) {
                                    qqShareImage(this, it)
                                }
                            }
                        }, {
                            toast(it.msg)
                        })
                }
            }
        })
    }
}